package com.bw.dao;

import com.bw.pojo.User;
import com.bw.util.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/***************************
 FileAction:

 CreateTime:2022-04-07 9:58
 Author:Aaking-aline
 ****************************/
public class UserDao {

    // 查询方法
    public boolean selectUserByUsernameAndPassword(String username,String password) throws SQLException {
        String sql = "select * from user where username = ? and password = ?";
        Connection connection = JDBC.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);

        ps.setObject(1,username);
        ps.setObject(2,password);

        ResultSet rs = ps.executeQuery();

        while(rs.next()){
            return true;
        }
        return false;
    }

    // 增加方法
    public int insertUser(String username,String password) throws SQLException {
        String sql = "insert into user values(null,?,?)";
        Connection connection = JDBC.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setObject(1,username);
        ps.setObject(2,password);
        return ps.executeUpdate();
    }


    public boolean selectUserByUsername(String username) throws SQLException {
        String sql = "select * from user where username = ?";
        Connection connection = JDBC.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);

        ps.setObject(1,username);

        ResultSet rs = ps.executeQuery();

        while(rs.next()){
            return true;
        }
        return false;
    }
}
